<template>
  <div class="chart" ref="chartWrapRef">
    <chart
      ref="chartRef"
      :option="chartOption"
      :style="{ height: chartHeight }"
      @zrender:click="renderClickHandle"
    />
  </div>
</template>

<script>
import { defineComponent, ref, watch, computed, onMounted } from 'vue';
import { Chart } from '@/components';
import { getDataZoomVisibie, getDatazoomEnd } from '../util';
export default defineComponent({
  props: {
    data: {
      type: Array,
      default: () => [],
    },
    height: {
      type: Number,
      default: 300,
    },
    fieldNames: {
      type: Object,
      default: () => ({
        name: 'name',
        value: 'value',
      }),
    },
  },
  setup(props, { emit }) {
    const chartName = ref();
    const chartOption = ref({
      color: ['#E99220'],
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          // 坐标轴指示器，坐标轴触发有效
          type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
        },
      },
      title: {
        x: 'center',
        textStyle: {
          fontSize: '14',
          color: '#FF4747',
          fontWeight: '400',
        },
      },
      grid: {
        left: 50,
        right: 50,
        bottom: 50,
      },
      dataZoom: [
        {
          id: 'dataZoomX',
          show: true,
          showDataShadow: 'auto',
          xAxisIndex: [0],
          filterMode: 'filter',
          start: 0,
          end: 20,
          minValueSpan: 1,
          maxValueSpan: 20,
          orient: 'horizontal',
          zoomLock: false,
          height: 15,
          bottom: 0,
          borderColor: 'transparent', //  边框颜色
        },
      ],
      xAxis: {
        name: '手机号',
        type: 'category',
        axisTick: {
          alignWithLabel: true,
        },
        axisLabel: {
          interval: 'auto',
          inside: false,
          verticalAlign: 'middle',
          rotate: 30, // 角度
        },
        data: [],
      },
      yAxis: {
        name: '报警量',
        type: 'value',
        minInterval: 1,
        axisTick: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        splitLine: {
          lineStyle: {
            type: 'dashed',
          },
        },
      },
      series: {
        data: [],
        type: 'bar',
        barWidth: 20,
        itemStyle: {
          normal: {
            label: {
              show: true,
              position: 'top',
              textStyle: {
                fontWeight: 'bolder',
                fontSize: '12',
              },
            },
          },
        },
      },
    });

    const chartHeight = computed(() => `${props.height}px`);
    const chartRef = ref();
    const chartWrapRef = ref();

    const setChartOption = data => {
      const xAxis = [];
      const seriesData = [];
      chartName.value = '关联重点人员库：无';
      data.forEach(item => {
        if (item.status === 1) {
          item.itemStyle = {};
          item.itemStyle.color = '#F23333';
          chartName.value = '关联重点人员库：' + item.featureValue + '(' + item.personName + ')';
        }
        const name = item[props.fieldNames.name];
        const value = item[props.fieldNames.value];
        const seriesItem = {
          value,
        };
        Object.keys(item).forEach(
          o =>
            ![props.fieldNames.name, props.fieldNames.value].includes(o) &&
            (seriesItem[o] = item[o]),
        );

        xAxis.push(name);
        seriesData.push(seriesItem);
      });

      chartOption.value.xAxis.data = xAxis;
      chartOption.value.series.data = seriesData;
      chartOption.value.dataZoom[0].show = getDataZoomVisibie(chartWrapRef.value, data.length);
      if (getDatazoomEnd(chartWrapRef.value, data.length) == 0) {
        chartOption.value.dataZoom[0].end = 4;
      } else {
        chartOption.value.dataZoom[0].end = getDatazoomEnd(chartWrapRef.value, data.length);
      }
      chartOption.value.title.text = chartName.value;
      chartOption.value.series.data = chartOption.value.series.data.map(item => ({
        value: item.value,
        itemStyle: item.itemStyle,
      }));
    };

    const renderClickHandle = e => {
      if (e?.topTarget?.type === 'Rect') {
        const index = chartRef.value.getIndexByEvent(e);
        const item = props.data[index];
        emit('zrender:click', index, item);
      }
    };

    watch(
      () => props.data,
      () => {
        setChartOption(props.data);
      },
    );

    onMounted(() => {
      setChartOption(props.data);
    });

    return {
      chartOption,
      chartHeight,
      setChartOption,
      chartRef,
      chartWrapRef,
      renderClickHandle,
    };
  },
  components: {
    Chart,
  },
});
</script>

<style scoped></style>
